package com.kfh.ybracelet.sim;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kfh.ybracelet.R;
import com.kfh.ybracelet.adapter.OrderHistoryAdapter;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.Constant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 订单详情页面
 * Created by Aaron on 16/9/29.
 */
public class OrderInfoActivity extends BaseActivity {

    private TextView tvidinfo;
    private ListView lvhistory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_order_info);

        initView();

        String phoneNum = getIntent().getStringExtra("phoneNum");

        try {
            orderInfo(phoneNum);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initView() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        iv_left.setOnClickListener(this);

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.order_detail));

        this.lvhistory = (ListView) findViewById(R.id.lv_history);
        View headView = View.inflate(this, R.layout.layout_orderinfo_head, null);
        lvhistory.addHeaderView(headView);

        this.tvidinfo = (TextView) headView.findViewById(R.id.tv_id_info);

    }

    /**
     * 获取订单详情
     *
     * @param phoneNum
     */
    private void orderInfo(String phoneNum) throws JSONException {

        JSONObject request = new JSONObject();
        request.put("token", sp.getString(Constant.TOKEN, ""));
        request.put("phoneNum", phoneNum);

        HttpUtil.setPostRequest("请求中...", this, Constant.WEB_ROOT_PAY + Constant.PHONENUMINFO,
                request.toString(), getClass().getSimpleName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            JSONObject resutl = new JSONObject(str);

                            showInfo(resutl);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, null);

    }

    /**
     * @param resutl
     */
    private void showInfo(JSONObject resutl) throws JSONException {

        JSONObject identity = resutl.getJSONObject("identity");

        identity.getString("iName");
        identity.getString("iNo");
        identity.getString("iAddress");
        identity.getString("iTime");
        identity.getString("cardImg1");
        identity.getString("cardImg2");
        identity.getString("cardImg3");
        identity.getString("phoneNum");
        identity.getString("sendPeople");

        tvidinfo.setTextSize(12);
        tvidinfo.setText(
                "手机号:" + identity.getString("phoneNum")
                        + "\n姓名: " + identity.getString("iName")
                        + "\n身份证号: " + identity.getString("iNo")
                        + "\n家庭住址: " + identity.getString("iAddress")
                        + "\n有效日期: " + identity.getString("iTime")
//                        + "\n提交认证者: " + identity.getString("sendPeople")
//                        + "\n提交认证时间: " + identity.getString("addTime")
        );

        JSONArray cardList = resutl.getJSONArray("cardList");

        OrderHistoryAdapter historyAdapter = new OrderHistoryAdapter(this, cardList);
        lvhistory.setAdapter(historyAdapter);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;
        }
    }
}
